package com.kuaimacode.kframework.admin.controller.feedback;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.feedback.FeedbackService;
import com.kuaimacode.kframework.admin.services.user.UserService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.mapper.index.FeedbackMapper;
import com.kuaimacode.kframework.mybatis.models.index.Feedback;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author anzy
 * @date 2019/04/07
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/feedback"})
public class FeedbackController {
	private static String PAGE_FILE_NAME = "feedback/";

	@Autowired
	private FeedbackService feedbackServices;

	@Autowired
	private UserService userService;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						Feedback feedback,ModelMap model) {
		feedback.setType(Constant.FeedbackTypeEnum.FEEDBACK.getCode());
		PageInfo<Feedback> pageInfo = feedbackServices.findAll(pageNum, pageSize,feedback);
		// pageInfo.list feedback
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		Feedback feedback = new Feedback();
		map.put("feedback", feedback);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Feedback feedback = feedbackServices.getById(id);
		User user = userService.getById(feedback.getUserId());
		if(user !=null)feedback.setUsername(user.getUsername());
		String imgs = feedback.getImgs();
		String[] imgs_path = null;
		if(imgs!=null &&!"".equals(imgs)){
			imgs_path = imgs.split(",");
		}
		map.put("imgs",imgs_path);
		map.put("feedback", feedback);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(Feedback feedback, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				feedbackServices.save(feedback);
			}else{
				// 编辑保存
				feedback.setAcceptTime(new Date());
				feedback.setAcceptUserId(SessionUtil.getUserId());
				feedback.setState(3);
				feedbackServices.updateById(feedback);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		Feedback feedback = feedbackServices.getById(id);
		User user = userService.getById(feedback.getUserId());
		if(user !=null)feedback.setUsername(user.getUsername());
		String imgs = feedback.getImgs();
		String[] imgs_path = null;
		if(imgs!=null &&!"".equals(imgs)){
			imgs_path = imgs.split(",");
		}

		map.put("imgs",imgs_path);
		map.put("feedback", feedback);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			feedbackServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
